/*
 * Project Name spring-boot-tools
 * File Name LogonUserHolder
 * Package Name com.huxiaosu.tools.common.holder
 * Create Time 2024/7/15
 * Create by name：liujie
 */
package com.huxiaosu.tools.common.holder;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson2.JSONObject;
import com.huxiaosu.tools.common.base.LogonUser;
import com.huxiaosu.tools.common.constant.CommonConst;
import com.huxiaosu.tools.common.exception.PlatformException;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Description
 *
 * @author liujie
 * @date 2024/7/15 15:42
 */
@Slf4j
public class LogonUserHolder{
    private static final String LOGON_USER_KEY = "__USER_INFO__";

    /**
     * 存储用户登录信息到MDC
     * @param logonUser 登录用户信息
     * @author liujie
     * @date 2024/3/18 22:09
     */
    public static void set(LogonUser logonUser) {
        String strUser = JSONObject.toJSONString(logonUser);
        MDC.put(LOGON_USER_KEY, strUser);
    }


    public static LogonUser get() {
        try {
            String value = MDC.get(LOGON_USER_KEY);
            if (StringUtils.hasText(value)) {
                return JSONObject.parseObject(value, LogonUser.class);
            }
        }
        catch (Exception e) {
            log.error("getLogonUser", e);
        }
        return null;
    }

    public static void clear() {
        MDC.remove(LOGON_USER_KEY);
    }

    /**
     * 获取登录用户
     * @return java.util.List<java.lang.String>
     * @author liujie
     * @date 2023/4/27 10:12
     */
    public static LogonUser getLogonUser() {
        LogonUser logonUser = get();
        PlatformException.checkCondition(logonUser == null, "用户信息是空的");
        return logonUser;
    }

    /**
     * 获取登录用户角色
     * @return java.util.List<java.lang.String>
     * @author liujie
     * @date 2023/4/27 10:12
     */
    public static List<Integer> getRoleIds() {
        return CollUtil.isEmpty(getLogonUser().getRoleIds()) ? new ArrayList<>() : getLogonUser().getRoleIds();
    }

    /**
     * 获取登录用户 ID
     * @return java.lang.String
     * @author liujie
     * @date 2023/4/27 10:10
     */

    public static Integer getUserId() {
        return getLogonUser().getId();
    }

    /**
     * 获取登录用户所属组 ID
     * @return java.lang.String
     * @author liujie
     * @date 2023/4/27 10:10
     */
    public static List<Integer> getGroupId() {
        return getLogonUser().getGroupId();
    }

    /**
     * 获取登录租户 ID
     * @return java.lang.String
     * @author liujie
     * @date 2023/4/27 10:11
     */
    public static String getTenantId() {
        return getLogonUser().getTenantId();
    }

    /**
     * 登录是不是超级管理员
     */
    public static boolean isSupperAdmin() {
        List<Integer> roleIds = getRoleIds();
        if (CollUtil.isEmpty(roleIds)) {
            return false;
        }
        return roleIds.contains(CommonConst.SUPER_ADMIN_ROLE_ID);
    }

    /**
     * 登录是不是租户管理员
     */
    public static boolean isTenantAdmin() {
        List<Integer> roleIds = getRoleIds();
        if (CollUtil.isEmpty(roleIds)) {
            return false;
        }
        return roleIds.contains(CommonConst.TENANT_ADMIN_ROLE_ID);
    }
}